package com.chat.service.impl;

import com.chat.service.PythonScriptExecutorService;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;

@Service
public class PythonScriptExecutorImpl implements PythonScriptExecutorService {
    @Override
    //    输入疾病得到与之关联的Top10微生物
    public String getRes(String disease) {
        String ans = null;
        try {
            String pythonScriptExecutor = System.getProperty("user.dir") + "\\backend\\src\\main\\resources\\DMP\\DMP.py";
            String[] param = new String[] {"python", pythonScriptExecutor, disease};

            //构建进程，并指定脚本名称，路径，和参数
            ProcessBuilder processBuilder = new ProcessBuilder(param);
            processBuilder.redirectErrorStream(true);
            Process process = processBuilder.start();

            //获取脚本输出
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                ans = line;
            }
            bufferedReader.close();
            int exitCode = process.waitFor();
            System.out.println("Python运行返回结果为: " + exitCode);
        } catch (Exception e) {
            System.out.println("未找到文件");
        }
        System.out.println(ans);
        return ans;
    }

//    public static void main(String[] args) {
//        new PythonScriptExecutorImpl().getRes("Atopy");
//    }
}
